package com.myapp.ruyixing.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Message;
import com.myapp.ruyixing.model.ScoreMall;
import com.myapp.ruyixing.model.ScoreMall.ScoreExchangeItem;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.LoginHelper;

public class ScoreMallActivity extends BaseActivity {

	private ListView mListView;
	private TextView mEmptyView;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ScoreMall mall = (ScoreMall) o;

			if (mall != null && mall.items != null) {
				MyListAdapter adapter = new MyListAdapter(mall.items);
				mListView.setAdapter(adapter);
			}
			mListView.setEmptyView(mEmptyView);
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_score_tmall);

		initView();

		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);
		RYHttpClient.getScoreMall(this, mHandler);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		RYHttpClient.cancleRequest(this);
	}

	private void initView() {

		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ScoreExchangeItem item = (ScoreExchangeItem) mListView
						.getAdapter().getItem(position);
				if (item != null) {
					startActivity(new Intent(ScoreMallActivity.this,
							ScoreChangeDetailActivity.class).putExtra(
							"goodsid", item.goodsid));
				}
			}
		});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
	}

	private class ViewHolder {
		TextView mTitleView;
		TextView mChangeScoreView;
		TextView mChangeTimeView;
		TextView mContentView;
		ImageView mScoreView;
		NetworkImageView mPicView;
	}

	private class MyListAdapter extends ArrayAdapter<ScoreExchangeItem>
			implements OnClickListener {

		private RequestQueue mQueue;
		private ImageLoader mImageLoader;

		public MyListAdapter(List<ScoreExchangeItem> airlines) {
			super(ScoreMallActivity.this, -1, airlines);
			mQueue = Volley.newRequestQueue(ScoreMallActivity.this);
			mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_store_mall, parent, false);

				ViewHolder holder = new ViewHolder();
				holder.mTitleView = (TextView) convertView
						.findViewById(R.id.title);
				holder.mChangeScoreView = (TextView) convertView
						.findViewById(R.id.changescore);
				holder.mChangeTimeView = (TextView) convertView
						.findViewById(R.id.changetime);
				holder.mContentView = (TextView) convertView
						.findViewById(R.id.content);
				holder.mScoreView = (ImageView) convertView
						.findViewById(R.id.score);
				holder.mPicView = (NetworkImageView) convertView
						.findViewById(R.id.image);

				convertView.setTag(holder);
			}

			ViewHolder mHolder = (ViewHolder) convertView.getTag();

			ScoreExchangeItem item = getItem(position);

			mHolder.mTitleView.setText(item.title);
			mHolder.mContentView.setText(item.description);
			try {
				mHolder.mScoreView.setImageLevel(Integer.parseInt(item.score));
			} catch (Exception e) {
				e.printStackTrace();
			}
			mHolder.mChangeScoreView.setText("兑换积分：" + item.points);
			mHolder.mChangeTimeView.setText("兑换次数：" + item.changetimes);
			if (!TextUtils.isEmpty(item.picurl)) {
				mHolder.mPicView.setImageUrl(item.picurl, mImageLoader);
			}

			return convertView;

		}

		@Override
		public void onClick(View v) {
		}
	}
}
